package com.adcc.ags.gms.user.vo;

import com.adcc.ags.gms.core.vo.BaseVO;
import com.adcc.ags.gms.user.entity.GWUser;
import com.adcc.ags.gms.user.entity.Route;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;

import java.util.List;

/**
 * 用户视图
 */
public class GWUserVO extends BaseVO {

    // ID
    private long id;

    // 用户ID
    private String userId;

    // 用户名称
    private String userName;

    // 用户IP
    private String userIP;

    // 下行转发
    private boolean downLinkForward;

    // 发送队列ID
    private long sendQueueId;

    // 发送队列名称
    private String sendQueueName;

    // 接收队列ID
    private long recvQueueId;

    // 接收队列名称
    private String recvQueueName;

    // 下行报文(0:原文,1:拆分结果)
    private int downlinkMsg;

    // 状态
    private boolean status;

    // 用户路由配置
    private List<RouteVO> routeVOList = Lists.newArrayListWithExpectedSize(100);

    /**
     * 构造函数
     */
    public GWUserVO() {
    }

    /**
     * 构造函数
     * @param user
     */
    public GWUserVO(GWUser user){
        if(user != null){
            id = user.getId();
            userId = user.getUserId();
            userName = user.getUserName();
            userIP = user.getUserIP();
            downlinkMsg = user.getDownlinkMsg();
            if(user.getDownLinkForward() == 1){
                downLinkForward = true;
            }else{
                downLinkForward = false;
            }
            sendQueueId = user.getSendQueueId();
            sendQueueName = user.getSendQueueName();
            recvQueueId = user.getRecvQueueId();
            recvQueueName = user.getRecvQueueName();
            if(user.getStatus() == 1){
                status = true;
            }else {
                status = false;
            }
            if(user.getRoutes() != null && user.getRoutes().size() > 0){
                for(Route route : user.getRoutes()){
                    RouteVO vo = new RouteVO(route);
                    routeVOList.add(vo);
                }
            }
        }
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserIP() {
        return userIP;
    }

    public void setUserIP(String userIP) {
        this.userIP = userIP;
    }

    public boolean getDownLinkForward(){
        return downLinkForward;
    }

    public void setDownLinkForward(boolean downLinkForward) {
        this.downLinkForward = downLinkForward;
    }

    public long getSendQueueId() {
        return sendQueueId;
    }

    public void setSendQueueId(long sendQueueId) {
        this.sendQueueId = sendQueueId;
    }

    public String getSendQueueName() {
        return sendQueueName;
    }

    public void setSendQueueName(String sendQueueName) {
        this.sendQueueName = sendQueueName;
    }

    public long getRecvQueueId() {
        return recvQueueId;
    }

    public void setRecvQueueId(long recvQueueId) {
        this.recvQueueId = recvQueueId;
    }

    public String getRecvQueueName() {
        return recvQueueName;
    }

    public void setRecvQueueName(String recvQueueName) {
        this.recvQueueName = recvQueueName;
    }

    public int getDownlinkMsg() {
        return downlinkMsg;
    }

    public void setDownlinkMsg(int downlinkMsg) {
        this.downlinkMsg = downlinkMsg;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public List<RouteVO> getRouteVOList() {
        return routeVOList;
    }

    public void setRouteVOList(List<RouteVO> routeVOList) {
        this.routeVOList = routeVOList;
    }

    @Override
    public GWUser toEntity() {
        GWUser user = new GWUser();
        user.setId(id);
        user.setUserId(userId);
        user.setUserName(userName);
        user.setUserIP(userIP);
        user.setDownlinkMsg(downlinkMsg);
        if(downLinkForward){
            user.setDownLinkForward(1);
        }else{
            user.setDownLinkForward(0);
        }
        user.setSendQueueId(sendQueueId);
        user.setSendQueueName(sendQueueName);
        user.setRecvQueueId(recvQueueId);
        user.setRecvQueueName(recvQueueName);
        if(status){
            user.setStatus(1);
        }else{
            user.setStatus(0);
        }
        if(routeVOList != null && routeVOList.size() > 0){
            for(RouteVO vo : routeVOList){
                Route route = new Route();
                route.setId(vo.getId());
                route.setName(vo.getName());
                route.setAn(vo.getAn());
                route.setFi(vo.getFi());
                route.setSendAddress(vo.getSendAddress());
                route.setRecvAddress(vo.getRecvAddress());
                route.setSmi(vo.getSmi());
                route.setRgs(vo.getRgs());
                if(vo.getDestinationId() != null && vo.getDestinationId().size() > 0){
                    String strTemp = "";
                    for(int i = 0;i < vo.getDestinationId().size();i++){
                        if(i != vo.getDestinationId().size() - 1){
                            strTemp += vo.getDestinationId().get(i) + ",";
                        }else{
                            strTemp += vo.getDestinationId().get(i);
                        }
                    }
                    route.setDestination(strTemp);
                }
                if(vo.getGwUserVO() != null){
                   route.setGwUserId(vo.getGwUserVO().getId());
                }
            }
        }
        return user;
    }

    @Override
    public String toJson() throws JsonProcessingException {
        return new ObjectMapper().writeValueAsString(this);
    }
}
